package com.hig.risk.service.impl;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.hig.risk.domain.RiskSelfInspectionBasis;
import com.hig.risk.domain.RiskSelfInspectionStatic;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hig.risk.mapper.RiskSelfInspectionMapper;
import com.hig.risk.domain.RiskSelfInspection;
import com.hig.risk.service.IRiskSelfInspectionService;

/**
 * 重大隐患自查清单Service业务层处理
 * 
 * @author scp
 * @date 2024-09-29
 */
@Service
public class RiskSelfInspectionServiceImpl implements IRiskSelfInspectionService 
{
    @Autowired
    private RiskSelfInspectionMapper riskSelfInspectionMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询重大隐患自查清单
     * 
     * @param id 重大隐患自查清单ID
     * @return 重大隐患自查清单
     */
    @Override
    public RiskSelfInspection selectRiskSelfInspectionById(Long id)
    {
        RiskSelfInspection riskScoreControl = riskSelfInspectionMapper.selectRiskSelfInspectionById(id);
        if (riskScoreControl != null && StringUtils.isNotEmpty(riskScoreControl.getSource())){
            String[] hazardsTypes = riskScoreControl.getSource().split(",");
            riskScoreControl.setSourceIds(hazardsTypes);
        }
        return riskScoreControl;
    }

    /**
     * 查询重大隐患自查清单列表
     * 
     * @param riskSelfInspection 重大隐患自查清单
     * @return 重大隐患自查清单
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<RiskSelfInspection> selectRiskSelfInspectionList(RiskSelfInspection riskSelfInspection)
    {
        return riskSelfInspectionMapper.selectRiskSelfInspectionList(riskSelfInspection);
    }

    /**
     * 新增重大隐患自查清单
     * 
     * @param riskSelfInspection 重大隐患自查清单
     * @return 结果
     */
    @Override
    public int insertRiskSelfInspection(RiskSelfInspection riskSelfInspection)
    {
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        riskSelfInspection.setCreateBy(loginUser.getUser().getUserName());
//        riskSelfInspection.setDeptId(loginUser.getUser().getDeptId());
        riskSelfInspection.setCreateTime(DateUtils.getNowDate());
//        String[] sourceIds = riskSelfInspection.getSourceIds();
//        String source = String.join(",", Arrays.stream(sourceIds)
//                .map(String::valueOf)
//                .toArray(String[]::new));
//        riskSelfInspection.setSource(source);
        return riskSelfInspectionMapper.insertRiskSelfInspection(riskSelfInspection);
    }

    /**
     * 修改重大隐患自查清单
     * 
     * @param riskSelfInspection 重大隐患自查清单
     * @return 结果
     */
    @Override
    public int updateRiskSelfInspection(RiskSelfInspection riskSelfInspection)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        riskSelfInspection.setUpdateBy(loginUser.getUser().getUserName());
        riskSelfInspection.setUpdateTime(DateUtils.getNowDate());
//        String[] sourceIds = riskSelfInspection.getSourceIds();
//        String source = String.join(",", Arrays.stream(sourceIds)
//                .map(String::valueOf)
//                .toArray(String[]::new));
//        riskSelfInspection.setSource(source);
        return riskSelfInspectionMapper.updateRiskSelfInspection(riskSelfInspection);
    }

    /**
     * 批量删除重大隐患自查清单
     * 
     * @param ids 需要删除的重大隐患自查清单ID
     * @return 结果
     */
    @Override
    public int deleteRiskSelfInspectionByIds(Long[] ids)
    {
        return riskSelfInspectionMapper.deleteRiskSelfInspectionByIds(ids);
    }

    /**
     * 删除重大隐患自查清单信息
     * 
     * @param id 重大隐患自查清单ID
     * @return 结果
     */
    @Override
    public int deleteRiskSelfInspectionById(Long id)
    {
        return riskSelfInspectionMapper.deleteRiskSelfInspectionById(id);
    }

    @Override
    @DataScope(deptAlias = "d")
    public AjaxResult selectOvertime(RiskSelfInspection riskSelfInspection) {
        String msg = "";
        RiskSelfInspection inspection = riskSelfInspectionMapper.selectOvertime(riskSelfInspection);
        if (inspection != null ){
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
            String revisionTime = dateFormat.format(now);

            if (!revisionTime.equals(inspection.getRemarkSpare1())){
                msg = "当前月份没有录入重大隐患自查，请添加新的重大隐患自查！";
            }else {

            }
        }else {
            msg = "没有录入重大隐患自查，请添加重大隐患自查！";
        }
        if (msg.length()>0){
            return AjaxResult.error(msg);
        }
        return AjaxResult.success();
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<RiskSelfInspectionStatic> selectStaticList(RiskSelfInspection riskSelfInspection) {
        return riskSelfInspectionMapper.selectStaticList(riskSelfInspection);
    }

    @Override
    public int insertAllRiskSelfInspection(RiskSelfInspectionStatic riskSelfInspection) {
        RiskSelfInspection temp = new RiskSelfInspection();
        temp.setRemarkSpare1(riskSelfInspection.getRemarkSpare1());
        temp.setDeptId(riskSelfInspection.getDeptId());
        List<RiskSelfInspection> list = riskSelfInspectionMapper.selectRiskSelfInspectionList(temp);
        if (list != null && list.size()>0){
            throw new RuntimeException(riskSelfInspection.getRemarkSpare1() + "的重大隐患已经录入，请核查");
        }
        if (riskSelfInspection != null && riskSelfInspection.getBasisList() != null) {
            for (RiskSelfInspectionBasis basis : riskSelfInspection.getBasisList()) {
                if (basis != null && basis.getContentList() != null) {
                    for (RiskSelfInspection inspection : basis.getContentList()) {
                        inspection.setInspectionTime(basis.getInspectionTime());
                        inspection.setRemark(basis.getRemark());
                        inspection.setRemarkSpare1(riskSelfInspection.getRemarkSpare1());
                        inspection.setCreateBy(riskSelfInspection.getCreateBy());
                        inspection.setDeptId(riskSelfInspection.getDeptId());
                        inspection.setCreateTime(DateUtils.getNowDate());
                        riskSelfInspectionMapper.insertRiskSelfInspection(inspection);
                    }

                }
            }
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteRiskSelfInspection(RiskSelfInspectionStatic riskSelfInspection) {
        if (riskSelfInspection != null && riskSelfInspection.getBasisList() != null) {
            for (RiskSelfInspectionBasis basis : riskSelfInspection.getBasisList()) {

                    RiskSelfInspection inspection = new RiskSelfInspection();
                    inspection.setRemark(basis.getRemark());
                    inspection.setRemarkSpare1(basis.getRemarkSpare1());
                    inspection.setCreateBy(basis.getCreateBy());
                    inspection.setDeptId(basis.getDeptId());
                    riskSelfInspectionMapper.deleteRiskSelfInspection(inspection);

            }
            return 1;
        }
        return 0;
    }
}
